import withAuth from '../../../helpers/basicAuth';
import {
    visitVulnerabilityReports,
    vulnerabilityReportsConfigurationPath,
} from './VulnerabilityReporting.helpers';

const staticResponseMap = {
    'reports/configurations': {
        fixture: 'vulnerabilities/vulnerabilityReporting/vulnerabilityReports.json',
    },
};

function interceptVulnerabilityReport(reportId, response, interaction) {
    cy.intercept('GET', `/v2/reports/configurations/${reportId}`, response).as('reportDetails');

    interaction();
    cy.wait('@reportDetails');
}

describe('Vulnerability Reporting Navigation', () => {
    withAuth();

    it('navigates to the index page', () => {
        visitVulnerabilityReports(staticResponseMap);
        cy.location('pathname').should('eq', vulnerabilityReportsConfigurationPath);
        cy.get('h1').should('contain', 'Vulnerability reporting');
    });

    it('navigates to the create page', () => {
        visitVulnerabilityReports(staticResponseMap);
        cy.contains('button', 'Create report').click();
        cy.location().should((location) => {
            expect(location.pathname).to.eq(vulnerabilityReportsConfigurationPath);
            expect(location.search).to.include('action=create');
        });
        cy.get('h1').should('contain', 'Create report');
    });

    it('navigates to the details page', () => {
        visitVulnerabilityReports(staticResponseMap).then(([{ response }]) => {
            const firstReport = response.body.reportConfigs[0];

            interceptVulnerabilityReport(firstReport.id, firstReport, () => {
                cy.get('td').contains('a', firstReport.name).click();
            });

            cy.location('pathname').should(
                'eq',
                `${vulnerabilityReportsConfigurationPath}/${firstReport.id}`
            );
            cy.get('h1').should('contain', firstReport.name);
        });
    });

    it('navigates to the edit page', () => {
        visitVulnerabilityReports(staticResponseMap).then(([{ response }]) => {
            const firstReport = response.body.reportConfigs[0];
            interceptVulnerabilityReport(firstReport.id, firstReport, () => {
                cy.get('table button[aria-label="Kebab toggle"]').first().click();
                cy.contains('button', 'Edit report').click();
            });

            cy.location().should((location) => {
                expect(location.pathname).to.eq(
                    `${vulnerabilityReportsConfigurationPath}/${firstReport.id}`
                );
                expect(location.search).to.include('action=edit');
            });
            cy.get('h1').should('contain', 'Edit report');

            cy.get('nav[aria-label="Breadcrumb"] a').click();
            cy.get('td').contains('a', firstReport.name).click();

            cy.contains('button', 'Actions').click();
            cy.contains('button', 'Edit report').click();

            cy.location().should((location) => {
                expect(location.pathname).to.eq(
                    `${vulnerabilityReportsConfigurationPath}/${firstReport.id}`
                );
                expect(location.search).to.include('action=edit');
            });
            cy.get('h1').should('contain', 'Edit report');
        });
    });

    it('navigates to the clone page', () => {
        visitVulnerabilityReports(staticResponseMap).then(([{ response }]) => {
            const firstReport = response.body.reportConfigs[0];
            interceptVulnerabilityReport(firstReport.id, firstReport, () => {
                cy.get('table button[aria-label="Kebab toggle"]').first().click();
                cy.contains('button', 'Clone report').click();
            });

            cy.location().should((location) => {
                expect(location.pathname).to.eq(
                    `${vulnerabilityReportsConfigurationPath}/${firstReport.id}`
                );
                expect(location.search).to.include('action=clone');
            });
            cy.get('h1').should('contain', 'Clone report');

            cy.get('nav[aria-label="Breadcrumb"] a').click();
            cy.get('td').contains('a', firstReport.name).click();

            cy.contains('button', 'Actions').click();
            cy.contains('button', 'Clone report').click();

            cy.location().should((location) => {
                expect(location.pathname).to.eq(
                    `${vulnerabilityReportsConfigurationPath}/${firstReport.id}`
                );
                expect(location.search).to.include('action=clone');
            });
            cy.get('h1').should('contain', 'Clone report');
        });
    });
});
